package com.jww.controller.store;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.jww.controller.BaseServlet;
import com.jww.domain.store.Catalog;
import com.jww.domain.store.Company;
import com.jww.domain.store.Course;
import com.jww.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/catalog/*")
public class CatalogServlet extends BaseServlet {

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");

        List<Catalog> list = catalogService.findAll(pageNum, pageSize);
        System.out.println("===============" + list);

        //  将数据交由分页插件处理获取分页信息
        PageInfo<Catalog> pageInfo = new PageInfo<>(list);
        //将结果传回来也就是存入作用域
        req.setAttribute("page", pageInfo);

        //页面跳转
        req.getRequestDispatcher("/pages/store/catalog/list.jsp").forward(req, resp);

    }

    /**
     * 删除功能
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    private void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取批量删除的id
        String ids = req.getParameter("ids");
        // 调用service
        boolean flag = catalogService.deleteByIds(ids);
        if (flag) {
            findAll(req, resp);
        } else {
            //跳转到错误页面
            //无
        }

    }

    /**
     * 增加
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    private void save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取页面数据封装到指定对象中去
        Catalog catalog = BeanUtil.fillBean(req, Catalog.class);
        catalogService.save(catalog);
        //调用findAll方法
        findAll(req, resp);

    }

    //   不带分页的查询所有
    private void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Catalog> list = catalogService.list();

        req.setAttribute("courseList", list);
        req.getRequestDispatcher("/pages/store/catalog/add.jsp").forward(req, resp);

    }

    /**
     * 根据id查询当前企业信息，并跳转到修改页面
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取当前id
        String id = req.getParameter("id");
        List<Course> courseList = courseService.list();
        Catalog catalog = catalogService.findById(id);
        req.setAttribute("catalog", catalog);
        req.setAttribute("courseList", courseList);

        req.getRequestDispatcher("/pages/store/catalog/update.jsp").forward(req, resp);

    }

    /**
     * 真的修改
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取修改页面表单提交的数据，并封装到Company对象
        Catalog catalog = BeanUtil.fillBean(req, Catalog.class);
        catalogService.update(catalog);
        findAll(req, resp);
    }

}
